package com.taotao.manage.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.manage.pojo.ContentCategory;
import com.taotao.manage.service.ContentCategoryService;

/**
 * @Description:内容类目控制层
 * @author:     mingxu
 */

@RequestMapping("content/category")
@Controller
public class ContentCategoryController {

	@Autowired
	private ContentCategoryService contendCategoryService;

	/**  查询所有一级内容类目
	 * 			select * from tb_content_category where parent_id=0
	 *	请求地址：content/category GET请求
	 *	请求参数：前端框架会传递一个id，查询所有parentId=0的内容类目
	 *	响应结果：List<ContentCategory>
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<List<ContentCategory>> queryContentCategory(
			@RequestParam(value = "id", defaultValue = "0") Long parentId) {
		try {
			ContentCategory record = new ContentCategory();

			//设置查询父节点，以及状态为1即有效的数据
			record.setParentId(parentId);
			record.setStatus(1);

			//查询所有的内容类目
			List<ContentCategory> contentCategoryList = contendCategoryService.queryListByWhere(record);

			if (contentCategoryList == null) {
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			return ResponseEntity.status(HttpStatus.OK).body(contentCategoryList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}

	/**
	 *	添加内容类目:
	 *		1.添加内容类目
	 *			insert into tb_content_category values();
	 *		2.修改父类目的isParent=true
	 *			update tb_content_category set isParent=true where parentId=?
	 *
	 *		两次操作需要在同一个事务中，所以在控制层一次操作，在业务层分开两次操作
	 *
	 *	请求路径：content/category POST
	 *	请求参数：parentId name
	 *	响应数据：Void  201 500
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<ContentCategory> addContentCategory(ContentCategory record) {
		try {
			if (record.getName() == null || StringUtils.isBlank(record.getName())) {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
			}

			//调用业务层添加内容类目,并更新父节点的isParent=true
			ContentCategory result = contendCategoryService.addContendCategoryAndUpdateParent(record);
			return ResponseEntity.status(HttpStatus.CREATED).body(result);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);

	}

	/**
	 * @Description:修改内容分类
	 * @author mingxu
	 */
	@RequestMapping(method = RequestMethod.PUT)
	public ResponseEntity<Void> updateContentCategory(@RequestParam("id") Long id, @RequestParam("name") String name) {
		try {
			ContentCategory record = new ContentCategory();
			record.setId(id);
			record.setName(name);
			contendCategoryService.updateById(record);
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

	}

	/**
	 * @Description:删除内容分类
	 * 		如果是子节点，直接删除，如果是父节点，删除它以及其子节点
	 * @author mingxu
	 */
	@RequestMapping(method = RequestMethod.DELETE)
	public ResponseEntity<Void> deleteContentCategory(@RequestParam("parentId") Long parentId,
			@RequestParam("id") Long id) {
		try {
			contendCategoryService.deleteContentCategory(parentId, id);
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

	}

}
